package com.lsh.oms.core.model.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.lang.Exceptions;
import com.lsh.oms.core.enums.PayStatus;
import com.lsh.oms.core.enums.PayType;
import com.lsh.oms.core.model.base.VenderModel;
import com.lsh.oms.core.model.query.MisOrderHeadForPay;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.lsh.oms.core.enums.OrderStatus.ORDER_DELIVERY;
import static com.lsh.oms.core.enums.OrderStatus.ORDER_SORTING;

/**
 * 订单头
 *
 * @author huangdong
 * @date 16/8/1
 */
@Setter
@Getter
@NoArgsConstructor
@ToString
@EqualsAndHashCode
public class OrderHead extends VenderModel implements Serializable {

    private static final long serialVersionUID = -5325119665299947821L;

    /**
     * ID
     */
    private Long id;

    private String orderClass;

    /**
     * 订单号
     */
    private Long orderCode;

    /**
     * 父订单号
     */
    private Long parentOrderCode;

    /**
     * 订单类型
     */
    private Integer orderType;

    /**
     * 订单状态
     */
    private Integer orderStatus;

    /**
     * 区域编码
     */
    private Integer regionCode;

    /**
     * 区域销售码(2017-07-22 苗壮)
     */
    private String saleAreaCode;

    /**
     * 客户编码
     */
    private Long userCode;

    /**
     * 地址编码
     */
    private Long addressCode;

    /**
     * 省
     */
    private Integer province;

    /**
     * 市
     */
    private Integer city;

    /**
     * 区
     */
    private Integer district;

    /**
     * 片区
     */
    private Integer area;

    /**
     * 地址信息
     */
    private String addressInfo;

    /**
     * 商品总数量
     */
    private BigDecimal goodsTotalQuantity;

    /**
     * 商品总金额
     */
    private BigDecimal goodsTotalAmount;

    /**
     * 商品优惠金额
     */
    private BigDecimal goodsDiscountAmout;

    /**
     * 订单折扣率
     */
    private BigDecimal orderDiscountRate;

    /**
     * 订单优惠金额
     */
    private BigDecimal orderDiscountAmout;

    /**
     * 优惠券总金额
     */
    private BigDecimal ticketCouponAmout;

    /**
     * 现金券总金额
     */
    private BigDecimal cashCouponAmount;

    /**
     * 运费
     */
    private BigDecimal freight;

    /**
     * 下单时间
     */
    private Integer orderTime;

    /**
     * 支付类型（1-货到付款，2-在线支付）
     */
    private Integer payType;

    /**
     * 支付状态（0：未支付，1-已支付）
     */
    private Integer payStatus;

    /**
     * 支付平台类型 1-支付宝, 2-微信，5-拉卡拉，6-微信扫码，8-支付宝扫码 9 赊账
     */
    private Integer payChannel;

    /**
     * 支付流水号
     */
    private String paySequence;

    /**
     * 支付时间
     */
    private Integer payTime;

    /**
     * 抹零金额
     */
    private BigDecimal floorAmount;

    /**
     * 支付减免金额
     */
    private BigDecimal payReliefAmout;

    /**
     * 应付金额
     */
    private BigDecimal expectPayAmount;

    /**
     * 实付金额
     */
    private BigDecimal actualPayAmount;

    /**
     * 仓储类型
     */
    private Integer storageType;

    /**
     * 是否美批订单
     */
    private Integer isMp;

    /**
     * SO单号
     */
    private String soCodes;

    /**
     * 库存流水号
     */
    private String atpFlowCode;

    /**
     * 创建时间
     */
    private Integer createTime;

    /**
     * 更新时间
     */
    private Integer updateTime;

    /**
     * 是否有效（0:无效；1:有效）
     */
    private Integer valid;

    /**
     * 缺交类型
     */
    private Integer lackDeliveryType;

    /**
     * 商城订单号
     */
    private String misOrderNo;
    /**
     * ext
     */
    private String ext;

    private String providerId;
    /**
     * extObject
     */
    private OrderHeadExt extObject;

    private String supplierGroupId;
    /**
     * 订单支付流水信息
     */
    private List<MisOrderHeadForPay> paySequences;

    private List<OrderDetail> details;


    public OrderHead(Long orderCode, Integer orderStatus) {
        this.orderCode = orderCode;
        this.orderStatus = orderStatus;
    }

//    public OrderHead(OtherSysUpdateDto dto) {
//        this.orderCode = dto.getOrderNo();
//        this.orderStatus = dto.getStatus();
//    }

    public boolean paid() {
        return PayStatus.PAID.getValue().equals(payStatus);
    }

    public boolean delivering() {
        return orderStatus != null && (orderStatus.equals(ORDER_DELIVERY.getIndex()) || orderStatus.equals(ORDER_SORTING.getIndex()));
    }

    public boolean hasPaymentNo() {
        return payType.equals(PayType.ONLINE_PAYMENT.getValue()) || payType.equals(PayType.OPEN_ACCOUNT.getValue()) || payType.equals(PayType.PRE_PAYMENT.getValue());
    }

    public boolean hasParentOrder() {
        return parentOrderCode != null && parentOrderCode != 0;
    }

    public Map<String, Object> extendedProperties() {
        if (StringUtils.isNoneBlank(this.ext)) {
            try {
                return JSON.parseObject(this.ext);
            } catch (Exception e) {
                Exceptions.printStackTraceLineByLine(e);
            }
        }
        return new JSONObject();
    }

    public JSONObject addressProperties() {
        if (StringUtils.isNoneBlank(this.addressInfo)) {
            return JSON.parseObject(this.addressInfo);
        }
        return new JSONObject();
    }
}
